// packagePay/pages/black-list/black-list.js
const app = getApp()
import {
    getGodWorkers
} from '../../api/black'
import user from '../../../utils/user.js'
Page({

    /**
	 * 页面的初始数据
	 */
    data: {
        showAddStatus: false,
        blacks: [],
        page: 1,
        totalRow: 100,
        searchText: '',
        islogin: false,
        totalCount:true
    },

    /**
	 * 生命周期函数--监听页面加载
	 */
    onLoad: function() {
        user.waitLogin().then(()=>{
            this.setData({
                islogin: true
            })
        })
        this.getGodWorkers()
    },
    search(event){
        this.setData({
            page: 1,
            searchText: event.detail.value,
            blacks: []
        })
        this.getGodWorkers()
    },
    // 无限列表
    scrollDown(){
        if(!this.data.hasNextPage){
            app.toast('暂无更多信息')
            return 0
        }
        console.log('scroll')
        this.setData({
            page: this.data.page+1,
        })
        this.getGodWorkers()
    },
    getGodWorkers(){
        getGodWorkers({
            pageNum: this.data.page,
            pageSize: 50,
            searchText: this.data.searchText,
        }).then(res => {
            console.log(res)
            const totalRow = res.totalRow
            res = res.data
            for(const i in res){
                if(res[i].idCardNo)
                    res[i].idCardNo = res[i].idCardNo.replace(/^(.{4})(?:\d+)(.{4})$/, '$1******$2')
                if(res[i].companyName !== '排雷大队')
                    res[i].companyName = res[i].companyName.replace(/^(.{1})(?:.+)(.{1})$/, '$1**$2')
            }
            const blacks = this.data.blacks
            blacks.push(...res)
            this.setData({
                blacks,
                totalRow,
                hasNextPage:res.hasNextPage
            })
        })
    },
    // 未登录阻止搜索
    islogin(event){
        console.log(event)
        if(!user.userId){
            app.confirm('需要登录后才能使用搜索功能，是否立即登录？').then(()=>{
                user.goRegister()
            })
        }
    },
    addComplete(){
        this.setData({
            blacks: []
        })
        this.getGodWorkers()
        this.setData({
            showAddStatus: false
        })
    },
    onPreview(event){
        wx.previewImage({
            urls: this.data.blacks[event.currentTarget.dataset.index].picture,
            current: event.currentTarget.dataset.index2
        })
    },
    showAdd(){
        if(!user.userId){
            app.confirm('需要登录后才能使用录入功能，是否立即登录？').then(()=>{
                user.goRegister()
            })
            return 0
        }
        this.setData({
            showAddStatus: true
        })
    },
	
    hideAdd(){
        this.setData({
            showAddStatus: false
        })
    },
    onShareAppMessage() {
        return {
            title: '大神套路深，坚决不跳坑（附劳务市场大神员工名单）',
            imageUrl: '/packageOther/assets/img/black-list/share.png'
        }
    },
    onShareTimeline(){
        return {
            title: '大神套路深，坚决不跳坑（附劳务市场大神员工名单）',
            imageUrl: '/packageOther/assets/img/black-list/share.png'
        }
    }
})
